package org.soulraven.games.memory.ui.config;

import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JToggleButton;

import org.soulraven.games.memory.ui.config.controller.ConfigController;
import org.soulraven.games.memory.util.ImageUtil;
import org.soulraven.games.memory.util.MessageUtil;

public class ConfigFrame extends JFrame {

	private static final long serialVersionUID = 8889162352740486745L;

	private static final String SINGLE_PLAYER_PANEL = "SINGLE_PLAYER_PANEL";
	private static final String TWO_PLAYERS_PANEL = "TWO_PLAYERS_PANEL";

	private JPanel northPanel = null;
	private JPanel centerPanel = null;
	private JPanel southPanel = null;
	private SinglePlayerPanel singlePlayerPanel = null;
	private TwoPlayersPanel twoPlayersPanel = null;
	private JToggleButton singlePlayerButton = null;
	private JToggleButton twoPlayersButton = null;
	private JButton okButton = null;
	private JButton cancelButton = null;
	private ConfigPanel currentPanel = null;
	private ConfigController controller = null;

	public ConfigFrame() {
		this.controller = new ConfigController(this);
		initialize();
	}

	private void initialize() {
		this.setIconImage(ImageUtil.getFavicon().getImage());
		this.setTitle(MessageUtil.getMessage("configtitle"));
		this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		this.addWindowListener(controller);
		this.currentPanel = getSinglePlayerPanel();
		this.setLayout(new BorderLayout(5, 5));
		this.add(getNorthPanel(), BorderLayout.NORTH);
		this.add(getCenterPanel(), BorderLayout.CENTER);
		this.add(getSouthPanel(), BorderLayout.SOUTH);
		this.setResizable(Boolean.FALSE);
		this.setLocationRelativeTo(null);
		this.pack();
	}

	/**
	 * @return the northPanel
	 */
	private JPanel getNorthPanel() {
		if (northPanel == null) {
			northPanel = new JPanel();
			northPanel.setLayout(new FlowLayout(FlowLayout.LEFT, 5, 5));
			northPanel.add(new JLabel(MessageUtil.getMessage("gamemode")));
			northPanel.add(getSinglePlayerButton());
			northPanel.add(getTwoPlayersButton());
		}
		return northPanel;
	}

	/**
	 * @return the centerPanel
	 */
	private JPanel getCenterPanel() {
		if (centerPanel == null) {
			centerPanel = new JPanel();
			centerPanel.setLayout(new CardLayout(5, 5));

			centerPanel.add(getSinglePlayerPanel(), SINGLE_PLAYER_PANEL);
			centerPanel.add(getTwoPlayersPanel(), TWO_PLAYERS_PANEL);
		}
		return centerPanel;
	}

	/**
	 * @return the southPanel
	 */
	private JPanel getSouthPanel() {
		if (southPanel == null) {
			southPanel = new JPanel();
			southPanel.setLayout(new FlowLayout(FlowLayout.RIGHT, 5, 5));
			southPanel.add(getOkButton());
			southPanel.add(getCancelButton());
		}
		return southPanel;
	}

	/**
	 * @return the okButton
	 */
	private JButton getOkButton() {
		if (okButton == null) {
			okButton = new JButton(MessageUtil.getMessage("ok"));
			okButton.addActionListener(controller);
			okButton.setName(ConfigController.OK_BUTTON);
		}
		return okButton;
	}

	/**
	 * @return the cancelButton
	 */
	private JButton getCancelButton() {
		if (cancelButton == null) {
			cancelButton = new JButton(MessageUtil.getMessage("cancel"));
			cancelButton.addActionListener(controller);
			cancelButton.setName(ConfigController.CANCEL_BUTTON);
		}
		return cancelButton;
	}

	/**
	 * @return the singlePlayerPanel
	 */
	private SinglePlayerPanel getSinglePlayerPanel() {
		if (singlePlayerPanel == null) {
			singlePlayerPanel = new SinglePlayerPanel();
		}
		return singlePlayerPanel;
	}

	/**
	 * @return the twoPlayersPanel
	 */
	private TwoPlayersPanel getTwoPlayersPanel() {
		if (twoPlayersPanel == null) {
			twoPlayersPanel = new TwoPlayersPanel();
		}
		return twoPlayersPanel;
	}

	/**
	 * @return the onePlayerButton
	 */
	private JToggleButton getSinglePlayerButton() {
		if (singlePlayerButton == null) {
			singlePlayerButton = new JToggleButton(MessageUtil.getMessage("oneplayer"));
			singlePlayerButton.addActionListener(new OnePlayerListener());
			singlePlayerButton.setSelected(Boolean.TRUE);
			singlePlayerButton.setEnabled(Boolean.FALSE);
		}
		return singlePlayerButton;
	}

	/**
	 * @return the twoPlayersButton
	 */
	private JToggleButton getTwoPlayersButton() {
		if (twoPlayersButton == null) {
			twoPlayersButton = new JToggleButton(MessageUtil.getMessage("twoplayers"));
			twoPlayersButton.addActionListener(new TwoPlayersListener());
			// TODO REMOVE WHEN TWO PLAYERS BE ENABLED
			twoPlayersButton.setEnabled(Boolean.FALSE);
		}
		return twoPlayersButton;
	}

	/**
	 * @return the currentPanel
	 */
	public ConfigPanel getCurrentPanel() {
		return currentPanel;
	}

	private class OnePlayerListener implements ActionListener {
		@Override
		public void actionPerformed(ActionEvent e) {
			CardLayout cardLayout = (CardLayout) getCenterPanel().getLayout();
			cardLayout.show(getCenterPanel(), SINGLE_PLAYER_PANEL);
			currentPanel = getSinglePlayerPanel();
			getSinglePlayerButton().setEnabled(Boolean.FALSE);
			JToggleButton twoPButton = getTwoPlayersButton();
			twoPButton.setSelected(Boolean.FALSE);
			twoPButton.setEnabled(Boolean.TRUE);
		}
	}

	private class TwoPlayersListener implements ActionListener {
		@Override
		public void actionPerformed(ActionEvent e) {
			CardLayout cardLayout = (CardLayout) getCenterPanel().getLayout();
			cardLayout.show(getCenterPanel(), TWO_PLAYERS_PANEL);
			currentPanel = getTwoPlayersPanel();
			getTwoPlayersButton().setEnabled(Boolean.FALSE);
			JToggleButton singlePButton = getSinglePlayerButton();
			singlePButton.setSelected(Boolean.FALSE);
			singlePButton.setEnabled(Boolean.TRUE);
		}
	}
}
